﻿using Customer.Read.API.Dtos;
using Customer.Domain;
using Customer.Read.API.Applcation.Command.FinanceTypeCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;

namespace Customer.Read.API.Controllers
{
    /// <summary>
    /// 财务类别 控制器 读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    
    public class FinanceTypeController : ControllerBase
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public FinanceTypeController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 反填财务类别
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FinanceType>> FanFinanceType([FromQuery]FanFinanceTypeCommand command)
        {
            ApiResult<FinanceType> result = new ApiResult<FinanceType>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示财务类别
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<FinanceType>>> GetFinanceType([FromQuery]SelectFinanceTypeCommand command)
        {
            ApiResult<List<FinanceType>> result = new ApiResult<List<FinanceType>>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.User_Id=Convert.ToInt32(uid);
                command.Office_Id=sid;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 财务类型导出
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileContentResult> GetFinanceDao([FromQuery]SelectFinanceTypeCommand command)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            command.User_Id = Convert.ToInt32(uid);
            command.Office_Id = sid;
            var obj = await _mediator.Send(command);
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("财务分类");
            IRow row;
            row=sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("财务类型ID");
            row.CreateCell(1).SetCellValue("财务类型编号");
            row.CreateCell(2).SetCellValue("财务类型名称");
            row.CreateCell(3).SetCellValue("财务类型备注");
            int index = 1;
            foreach (var item in obj)
            {
                row = sheet.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.FinanceTypeId);
                row.CreateCell(1).SetCellValue(item.FinanceTypeCode);
                row.CreateCell(2).SetCellValue(item.FinanceTypeName);
                row.CreateCell(3).SetCellValue(item.FRremark);
                index++;
            }
            byte[] buffe;
            using(MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                buffe = ms.ToArray();
            }
            string dc = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(buffe, dc, "财务类别表.xlsx");
        }





        ////////////////////////////////////////////
        ///////////////////////////////////////////
        ////////////////反填应收管理////////////////////////////
        ///////////////////////////////////////////
        /// <summary>
        /// 反填财务应收
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Receivable>> FanNewPaid([FromQuery]FanReceivableCommand command)
        {
            ApiResult<Receivable> result = new ApiResult<Receivable>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示财务应收
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Receivablepage>> GetReceivablepage([FromQuery]SelectReceivableCommand command)
        {
            ApiResult<Receivablepage> result = new ApiResult<Receivablepage>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.User_Id = Convert.ToInt32(uid);
                command.Office_Id = sid;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 财务类型导出
        /// </summary>
        /// <param name="select"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileContentResult> GetReceivable([FromQuery] SelectReceivableCommand select)
        {
            var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
            var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
            select.User_Id = Convert.ToInt32(uid);
            select.Office_Id = sid;
            var objs = await _mediator.Send(select);
            IWorkbook wb = new XSSFWorkbook();
            ISheet sheet = wb.CreateSheet("财务应收");
            IRow row;
            row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("应收管理id");
            row.CreateCell(1).SetCellValue("流水号");
            row.CreateCell(2).SetCellValue("名称");
            row.CreateCell(3).SetCellValue("类型");
            row.CreateCell(4).SetCellValue("最后付款日");
            row.CreateCell(5).SetCellValue("付款方");
            row.CreateCell(6).SetCellValue("收款方");
            row.CreateCell(7).SetCellValue("应收金额");
            row.CreateCell(8).SetCellValue("付款次数");
            row.CreateCell(9).SetCellValue("状态");
            row.CreateCell(11).SetCellValue("备注");
            int index = 1;
            foreach (var item in objs.list)
            {
                row = sheet.CreateRow(index);
                row.CreateCell(0).SetCellValue(item.ReceivableId);
                row.CreateCell(1).SetCellValue(item.SerialNumber);
                row.CreateCell(2).SetCellValue(item.ReceivableName);
                row.CreateCell(3).SetCellValue((double)item.FinanceTypeId);
                row.CreateCell(4).SetCellValue(item.Lastpaydate.ToString());
                row.CreateCell(5).SetCellValue(item.Payer);
                row.CreateCell(6).SetCellValue(item.Beneficiary);
                row.CreateCell(7).SetCellValue((double)item.ReceivableAmount);
                row.CreateCell(8).SetCellValue((int)item.PaymentNum);
                row.CreateCell(9).SetCellValue((double)item.ReceivableState);
                row.CreateCell(11).SetCellValue(item.RRremark);
                index++;
            }
            byte[] buffe;
            using (MemoryStream ms = new MemoryStream())
            {
                wb.Write(ms);
                buffe = ms.ToArray();
            }
            string dc = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(buffe, dc, "财务应收表.xlsx");
        }







    }
}
